//
//  HLxiaoxizhongxiTTableViewController.m
//  huiliantong
//
//  Created by hlt on 15/9/17.
//  Copyright (c) 2015年 hlt. All rights reserved.
//

#import "HLxiaoxizhongxiTTableViewController.h"
#import "HLTxiaoxizhongxinTableViewCell.h"
#import "MJRefresh.h"
@interface HLxiaoxizhongxiTTableViewController ()


@property NSInteger count;


@end

@implementation HLxiaoxizhongxiTTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _count=0;
    
    self.xiaoxilist=[[NSMutableArray alloc]initWithCapacity:3];
    
    
    [self setUpTableView];
    
    
    //去掉多余分割线的方法
    UIView *view = [UIView new];
    
    view.backgroundColor = [UIColor clearColor];
    
    [self.tableView setTableFooterView:view];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}






-(void)setUpTableView{
    //添加下拉的动画图片
    //设置下拉刷新回调
    [self.tableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(getFirstPageData)];
    
    //设置普通状态的动画图片
//    NSMutableArray *idleImages = [NSMutableArray array];
//    for (NSUInteger i = 1; i<=60; ++i) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%zd",i]];
//        [idleImages addObject:image];
//    }
//    [self.tableView.gifHeader setImages:idleImages forState:MJRefreshHeaderStateIdle];
//    
//    //设置即将刷新状态的动画图片
//    NSMutableArray *refreshingImages = [NSMutableArray array];
//    for (NSInteger i = 1; i<=3; i++) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%zd",i]];
//        [refreshingImages addObject:image];
//    }
//    [self.tableView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStatePulling];
//    
//    //设置正在刷新是的动画图片
//    [self.tableView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStateRefreshing];
    
    //马上进入刷新状态
    [self.tableView.gifHeader beginRefreshing];
    
    
    //上拉刷新
    [self.tableView addGifFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    //隐藏状态文字
    //    self.tableView.footer.stateHidden = YES;
    //设置正在刷新的动画
//    self.tableView.gifFooter.refreshingImages = refreshingImages;
    
    
    
    
}



-(void)getFirstPageData{
    _count = 0;
    [self refreshData];
}

-(void)refreshData{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        
        [self getxiaoxilists];
        
        //        [self getMerchantList];
    });
}

-(void)loadMoreData{
    NSLog(@"2222  offset:%zd",_count);
    _count = _count + 10;
    NSLog(@"3333  offset:%zd",_count);
    [self refreshData];
}






-(void ) getxiaoxilists 
{
    NSInteger page=_count/10;
    if (page>0) {
        
        Account *acc=[[AccountTool sharedAccountTool]currentAccount];
        NSString *url=[huoquxiaoxi stringByAppendingFormat:@"&username=%@&type=%ld&page=%ld",acc.custPhone,(long)self.type,(long)page];
        //将url转换一下格式，否则请求的格式就不兼容
        //    url = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
        [HLTNetWorkingtool getary:url success:^(NSArray *ary) {
            [self.xiaoxilist addObjectsFromArray:ary];
            
            self.count=self.count-10+[ary count];
            [self.tableView reloadData];
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
        } faliure:^(NSString *error) {
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
        }];
    }
    else
    {
    
        Account *acc=[[AccountTool sharedAccountTool]currentAccount];
        NSString *url=[huoquxiaoxi stringByAppendingFormat:@"&username=%@&type=%ld&page=%ld",acc.custPhone,(long)self.type,(long)page];
    //将url转换一下格式，否则请求的格式就不兼容
    //    url = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    [HLTNetWorkingtool getary:url success:^(NSArray *ary) {
        [self.xiaoxilist removeAllObjects];
        [self.xiaoxilist addObjectsFromArray:ary];
        self.count=[ary count];
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    } faliure:^(NSString *error) {
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    }];
        
    }
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return _count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identify1 = @"HLTxiaoxizhongxinTableViewCell";
    
    
    HLTxiaoxizhongxinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify1];
    
    if(cell == nil)
    {
        cell= [[[NSBundle mainBundle]loadNibNamed:@"HLTxiaoxizhongxinTableViewCell" owner:nil options:nil] firstObject];
        
    }
    //    cell.backgroundColor=[UIColor clearColor];//关键语句
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    //一以下是将秒数的时间转化为字符串的额时间
//    int intString = [self.xiaoxilist[indexPath.row][@"msg_time"] intValue];
//      NSDate *date=[NSDate dateWithTimeIntervalSince1970:intString ];
//    
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//    NSString *destDateString = [dateFormatter stringFromDate:date];
    cell.xiaoxidate.text=self.xiaoxilist[indexPath.row][@"a2"];
    cell.xiaoxiinfo.text=self.xiaoxilist[indexPath.row][@"a"];
    cell.orderid=self.xiaoxilist[indexPath.row][@"a3"];
    
//    cell.parent=self;
    
    return cell;
}



- (void)viewWillAppear:(BOOL)animated
{
    //在出现页面的时候先获取数据
    
    
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 60;
    
}



@end
